﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Entities;
using Distribution.Service;

namespace DistributionApplication
{
    public partial class frmTransformer : frmBase
    {
        #region - Properties -

        public EPole Pole { get; set; }
        public ETransformer TransformerToBeLoaded { get; set; }

        #endregion

        #region - Constructor -

        public frmTransformer()
        {
            InitializeComponent();
            ddlYear.Enabled = false;
        }

        #endregion

        protected override void lblLButton_Click(object sender, EventArgs e)
        {
            switch (tbcMain.SelectedIndex)
            {
                case 0:
                    tbcMain.SelectedIndex = 1;
                    break;
                case 1:
                    tbcMain.SelectedIndex = 2;
                    break;
                case 2:
                    tbcMain.SelectedIndex = 3;
                    break;
                case 3:
                    tbcMain.SelectedIndex = 4;
                    break;
                case 4:
                    tbcMain.SelectedIndex = 5;
                    break;
                case 5:
                    SaveMeter();
                    camControl.DisableCamera();
                    Close();

                    break;
                default:
                    break;
            }
        }

        protected override void lblRButton_Click(object sender, EventArgs e)
        {
            camControl.DisableCamera();
            Close();
        }

        private void frmTransformer_Load(object sender, EventArgs e)
        {
            Title = "PPI | Add Transformer";

            LButtonText = "Next";
            RButtonText = "Cancel";

            camControl.TempPhotoFileName = "TransformerPhoto";
            pGPSViewer.Gps = m_GPS;
            pGPSViewer.RefreshPosition();

            if (Settings.DistributionApplicationMode == DistributionApplicationMode.GPS) {
                txtComments.Visible = false;
                label4.Visible = false;
            }

            if (Pole != null && Pole.ValidPosition)
                pGPSViewer.SetPosition(Pole.Latitude, Pole.Longitude);

            LoadCombos();

            if (TransformerToBeLoaded != null)
                LoadTransformer(TransformerToBeLoaded);
        }

        private void LoadCombos()
        {
            List<ETransformerLookUp> transformerType = LLookUpService.GetAll(TransformerLookUpType.TransformerType);
            MobileUIHelper.FillDropdown(cmbTransformerType, transformerType.ToArray(), "[None]");

            List<ETransformerLookUp> can = LLookUpService.GetAll(TransformerLookUpType.Can);
            MobileUIHelper.FillDropdown(ddlCan, can.ToArray(), "[None]");

            List<ETransformerLookUp> types = LLookUpService.GetAll(TransformerLookUpType.Type);
            MobileUIHelper.FillDropdown(cmbType2, types.ToArray(), "[None]");

            List<ETransformerLookUp> classes = LLookUpService.GetAll(TransformerLookUpType.Class);
            MobileUIHelper.FillDropdown(cmbClass, classes.ToArray(), "[None]");

            List<ETransformerLookUp> phases = LLookUpService.GetAll(TransformerLookUpType.Phase);
            MobileUIHelper.FillDropdown(cmbPhase, phases.ToArray(), "[None]");

            List<ETransformerLookUp> pcbTypes = LLookUpService.GetAll(TransformerLookUpType.PCBType);
            MobileUIHelper.FillDropdown(cmbPCBType, pcbTypes.ToArray(), "[None]");

            List<ETransformerLookUp> arrestorKvs = LLookUpService.GetAll(TransformerLookUpType.ArrestorKv);
            MobileUIHelper.FillDropdown(ddlArrestorKv, arrestorKvs.ToArray(), "[None]");

            List<ETransformerLookUp> cutOutsKvs = LLookUpService.GetAll(TransformerLookUpType.CutoutKv);
            MobileUIHelper.FillDropdown(ddlCutout, cutOutsKvs.ToArray(), "[None]");
            
            List<ETransformerLookUp> determinedBy = LLookUpService.GetAll(TransformerLookUpType.DeterminedBy);
            MobileUIHelper.FillDropdown(cmbDeterminedBy, determinedBy.ToArray(), string.Empty);

            List<ETransformerLookUp> keyboards = LLookUpService.GetAll(TransformerLookUpType.Keywords);
            List<ELookUp> lookupList = new List<ELookUp>();

            MobileUIHelper.FillNumericCombo(ddlMonth, 1, 12, 1, true, "[None]");
            MobileUIHelper.FillNumericCombo(ddlYear, DateTime.Now.Year, 1944, 1, false, "[None]");

            foreach (var k in keyboards)
            {
                var l = new ELookUp();
                l.Id = k.Id;
                l.Value = k.Id;
                l.Text = k.Text;
                l.Type = LookUpType.Top;

                lookupList.Add(l);
            }

            arKeywords.Description = "Notes";
            arKeywords.LoadEntities(lookupList, "Id", "Text", "[None]");

            if (Pole != null)
                txtPoleNumber.Text = Pole.Number;

            lblInspection.Text = DateTime.Now.ToString("MM/dd/yyyy");
            ddlArrestorKv.Enabled = false;
            ddlCutout.Enabled = false;
        }

        private void SaveMeter()
        {
            ETransformer transformer = new ETransformer();

            transformer.Pole = Pole;
            transformer.Comments = txtComments.Text.Trim();
            transformer.Latitude = pGPSViewer.Latitude;
            transformer.Longitude = pGPSViewer.Longitude;
            transformer.CreatedDate = DateTime.Now;

            transformer.TransformerType = cmbTransformerType.SelectedItem as ETransformerLookUp;
            transformer.Type = cmbType2.SelectedItem as ETransformerLookUp;
            transformer.Class = cmbClass.SelectedItem as ETransformerLookUp;
            transformer.Phase = cmbPhase.SelectedItem as ETransformerLookUp;
            transformer.Can = ddlCan.SelectedItem as ETransformerLookUp;
            transformer.DeterminedBy = cmbDeterminedBy.SelectedItem as ETransformerLookUp;
            transformer.TestNumber = txtTestNo.Text.Trim();
            transformer.Sticker = chkSticker.Checked;
            transformer.Impeddance = txtImpeddance.Text.Trim();
            transformer.Taps = txtTaps.Text.Trim();
            transformer.Polarity = txtPolarity.Text.Trim();
            transformer.UtilityNumber = txtUtilityNumber.Text.Trim();
            transformer.Manufacturer = txtManufacturer.Text.Trim();
            transformer.Primary = txtPrimary.Text.Trim();
            transformer.Secondary = txtSecondary.Text.Trim();
            transformer.Arrestor = chkArrestor.Checked;
            transformer.DualRated = chkDualRated.Checked;
            transformer.Pre1978 = chkPre1978.Checked;
            transformer.PPM = txtPPM.Text.Trim();
            transformer.PCBType = cmbPCBType.SelectedItem as ETransformerLookUp;
            transformer.PoleNumber = txtPoleNumber.Text;
            transformer.ArrestorKv = ddlArrestorKv.SelectedItem as ETransformerLookUp;
            transformer.Cutout = chkCutout.Checked;
            transformer.CutoutKv = ddlCutout.SelectedItem as ETransformerLookUp;

            if (ddlYear.Enabled)
                transformer.ManufDate = new DateTime(Convert.ToInt32(ddlYear.SelectedItem), Convert.ToInt32(ddlMonth.SelectedItem), 1);
            else
                transformer.ManufDate = DateTime.MinValue;

            var notes = arKeywords.GetSelected();
            transformer.Notes = new List<ETransformerLookUp>();
            foreach (var n in notes)
            {
                var k = new ETransformerLookUp();

                k.Id = n.Id;
                k.Text = n.Text;
                k.Type = TransformerLookUpType.Keywords;

                transformer.Notes.Add(k);
            }

            List<EImage> photos = camControl.GetTakenImages();
            transformer.Images = new List<EImage>();
            if (photos.Count > 0)
            {
                transformer.Images = photos;

                foreach (var image in transformer.Images)
                    image.ImageType = ImageType.Transformer;
            }

            transformer.SerialNumber = txtSerialNumber.Text.Trim();
            
            if (TransformerToBeLoaded == null)
                LTransformer.Save(transformer);
            else
            {
                transformer.Id = TransformerToBeLoaded.Id;
                LTransformer.Update(transformer);
            }
        }

        private void LoadTransformer(ETransformer transformer)
        {
            txtComments.Text = transformer.Comments;

            MobileUIHelper.SelectItem(cmbTransformerType, transformer.TransformerType);
            MobileUIHelper.SelectItem(cmbType2, transformer.Type);
            MobileUIHelper.SelectItem(cmbClass, transformer.Class);
            MobileUIHelper.SelectItem(cmbPhase, transformer.Phase);
            MobileUIHelper.SelectItem(cmbPCBType, transformer.PCBType);
            MobileUIHelper.SelectItem(ddlArrestorKv, transformer.ArrestorKv);
            MobileUIHelper.SelectItem(ddlCutout, transformer.CutoutKv);
            MobileUIHelper.SelectItem(ddlCan, transformer.Can);
            MobileUIHelper.SelectItem(cmbDeterminedBy, transformer.DeterminedBy);

            lblInspection.Text = transformer.CreatedDate.ToString("MM/dd/yyyy");
            txtUtilityNumber.Text = transformer.UtilityNumber;
            txtManufacturer.Text = transformer.Manufacturer;
            txtPrimary.Text = transformer.Primary;
            txtSecondary.Text = transformer.Secondary;
            chkArrestor.Checked = transformer.Arrestor;
            chkDualRated.Checked= transformer.DualRated;
            chkPre1978.Checked = transformer.Pre1978;
            txtPPM.Text = transformer.PPM;
            txtPoleNumber.Text = transformer.PoleNumber;
            txtTestNo.Text = transformer.TestNumber;
            txtImpeddance.Text = transformer.Impeddance;
            txtTaps.Text = transformer.Taps;
            txtPolarity.Text = transformer.Polarity;
            //dtpManufDate.Value = transformer.ManufDate;
            chkSticker.Checked = transformer.Sticker;
            chkCutout.Checked = transformer.Cutout;

            if (transformer.ManufDate != DateTime.MinValue)
            {
                //chkManDate.Checked = true;
                //dtpManufDate.Value = recloser.ManufDate;
                //dtpManufDate.Enabled = true;
                ddlMonth.SelectedItem = transformer.ManufDate.Month;
                ddlYear.SelectedItem = transformer.ManufDate.Year;
                ddlYear.Enabled = true;
            }
            else
                ddlYear.Enabled = false;


            ddlCutout.Enabled = transformer.Cutout;
            ddlArrestorKv.Enabled = transformer.Arrestor;

            List<ELookUp> lookupList = new List<ELookUp>();

            foreach (var k in transformer.Notes)
            {
                var l = new ELookUp();
                l.Id = k.Id;
                l.Value = k.Id;
                l.Text = k.Text;
                l.Type = LookUpType.Top;

                lookupList.Add(l);
            }

            arKeywords.Load(lookupList);

            if (transformer.ValidPosition)
                pGPSViewer.SetPosition(transformer.Latitude, transformer.Longitude);

            if (transformer.Images != null && transformer.Images.Count > 0)
            {
                camControl.SetTakenImages(transformer.Images);
                camControl.SelectPicture(0);
            }

            txtSerialNumber.Text = transformer.SerialNumber;
        }

        private void tbcMain_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            switch (tbcMain.SelectedIndex)
            {
                case 0:
                    camControl.DisableCamera();
                    LButtonText = "Next";
                    break;
                case 1:
                    camControl.DisableCamera();
                    LButtonText = "Next";
                    break;
                case 2:
                    camControl.DisableCamera();
                    LButtonText = "Next";
                    break;
                case 3:
                    camControl.DisableCamera();
                    LButtonText = "Next";
                    break;
                case 4:
                    camControl.DisableCamera();
                    LButtonText = "Next";
                    break;
                case 5:
                    if ((TransformerToBeLoaded == null || TransformerToBeLoaded.Images == null) && camControl.GetTakenImages().Count == 0)
                        camControl.ActiveCamera();

                    LButtonText = "Save";

                    break;
                default:
                    break;
            }
        }

        protected override void lblClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtSerialNumber_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtSerialNumber_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtComments_GotFocus_1(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtComments_LostFocus_1(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtUtilityNumber_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtUtilityNumber_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtManufacturer_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtManufacturer_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtPrimary_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtPrimary_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtSecondary_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtSecondary_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtPPM_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtPPM_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtComments_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtComments_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtPoleNumber_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtPoleNumber_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void chkArrestor_CheckStateChanged(object sender, EventArgs e)
        {
            ddlArrestorKv.Enabled = chkArrestor.Checked;

            if (!chkArrestor.Checked && ddlArrestorKv.Items.Count > 0)
                ddlArrestorKv.SelectedIndex = 0;
        }

        private void chkCutout_CheckStateChanged(object sender, EventArgs e)
        {
            ddlCutout.Enabled = chkCutout.Checked;

            if (!chkCutout.Checked && ddlCutout.Items.Count > 0)
                ddlCutout.SelectedIndex = 0;
        }

        private void txtTestNo_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtTestNo_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtImpeddance_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtImpeddance_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtPolarity_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtPolarity_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtTaps_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtTaps_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ddlMonth_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            ddlYear.Enabled = ddlMonth.SelectedIndex > 0;
        }
    }
}